from PowerClass.PowerBasic import PowerBasic
from setting import *
from lib.PowerLed import PowerLed
from lib.PowerSound import PowerSound
from lib.PowerScreen import PowerScreen
from lib.PowerControl import PowerControl
import time
import math 

class PowerTest(PowerBasic):
 
    def __init__(self):
        super().__init__()
        print("PowerTest init")
        self.screen=PowerScreen(POWER_PIN_OLED_I2C_SCL,POWER_PIN_OLED_I2C_SDA,500000,128,64)
        self.sound=PowerSound(POWER_PIN_SOURND_NO_SOURCE)
        self.led = PowerLed(POWER_PIN_LED_RED,POWER_PIN_LED_GREEN,POWER_PIN_LED_BLUE)
        self.control =PowerControl(POWER_PIN_PS1_Z,POWER_PIN_PS1_X,POWER_PIN_PS1_Y)
 
    def testLed(self):
        print("1.PowerTest testLed running")
        for num in range(5): 
            self.led.turnOnRedLed()
            time.sleep(0.2)
            self.led.turnOnGreeLed()
            time.sleep(0.2)
            self.led.turnOnBlueLed()
            time.sleep(0.2)

    def testSoundList(self):
        # 乐谱列表
        music = [1, 1, 5, 5, 6, 6, 5, 0,
                 4, 4, 3, 3, 2, 2, 1, 0,
                 5, 5, 4, 4, 3, 3, 2, 0,
                 5, 5, 4, 4, 3, 3, 2, 0,
                 1, 1, 5, 5, 6, 6, 5, 0,
                 4, 4, 3, 3, 2, 2, 1, 0,]
        self.sound.playToneList(music)
   
    def testSound(self,tone):
        self.sound.playTone(tone)
    
    def testScreenInit(self):
        self.screen.initScreen()
            
    def testScreenMsg(self,msg):
        self.screen.showMessage(msg)
    def testScreenRefresh(self):
        self.screen.showMenu(self.screen.menu_curr_List)
        self.screen.screenRefresh()
    def testControl(self):
        print(self.control.getX())
        print(self.control.getY())
        print(self.control.getButtonValue())
        print(self.control.IsButtonPress())
# 
# #         for num in range(10): 
#         self.screen.scorll()
# #             time.sleep(0.1)
            
            
            
            
if __name__ == '__main__':
       
    test = PowerTest()
    test.testScreenInit()
    test.testScreenMsg("1.SRC Done")
    test.testLed()
    test.testScreenMsg("2.LED Done")
    # test.testSoundList()
    test.testScreenMsg("3.Sound Done")

    ifInFunc = False
    while(True):
        
        time.sleep(0.3)
        test.screen.MoveMenu(test.control.getYDirect(),test.screen.menu_curr_List)
        option = test.screen.ZoomMenu(test.control.getXDirect(),test.screen.menu_curr_List)
     
        if(option != None):
           if (len(option)<=2):
             ifInFunc = False
             
        if (not(ifInFunc)):
           test.testScreenRefresh()
    #        print (option)
        if (option == ".1.1"):
            test.sound.playToneList([1])
            ifInFunc = True
            for i in range(1, 40):
                test.screen.oled.fill(0)
                test.screen.oled.cube(10,20,i)
                time.sleep(0.2)
                test.screen.oled.show()
        
        if (option == ".1.2"):
            test.sound.playToneList([2])
            ifInFunc = True
            test.screen.oled.fill(0)
            test.screen.oled.line(0,32,128,32,1)
            test.screen.oled.line(64,0,64,64,1)
            for i in range(1, 128):
                test.screen.oled.pixel(i,int(math.sin(i)*20),1)
                time.sleep(0.2)
                test.screen.oled.show()       

        if (option == ".1.3"):
            test.sound.playToneList([3])
            ifInFunc = True
            test.screen.oled.fill(0)
            test.screen.oled.line(0,32,128,32,1)
            test.screen.oled.line(64,0,64,64,1)
            for i in range(1, 128):
                test.screen.oled.pixel(i,int(math.sin(i)*20),1)
                time.sleep(0.2)
                test.screen.oled.show()
                
        if (option == ".2.1"):
            test.sound.playToneList([3])
            ifInFunc = True
            test.screen.oled.fill(0)
            test.screen.oled.text('Stepper test',1,0)
            test.screen.oled.show()
            